package com.bawan.location.controller;

import com.bawan.location.Service.ILocationService;
import com.bawan.location.bo.ServerLocationBo;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * Desc:
 * User: ZhengCongChun
 * Date: 13-11-5
 * Time: 上午10:10
 *
 * @Version 1.0
 */
@Controller
public class MapController {
    @Autowired
    ILocationService iLocationService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView getPages() {

        ModelAndView model = new ModelAndView("map");
        return model;

    }

    //return back json string
    @RequestMapping(value = "/getLocationByIpAddress", method = RequestMethod.GET)
    public @ResponseBody
    String getDomainInJsonFormat(@RequestParam String ipAddress) {

        ObjectMapper mapper = new ObjectMapper();

        ServerLocationBo location = iLocationService.getLocation(ipAddress);

        String result = "";

        try {
            result = mapper.writeValueAsString(location);
        } catch (Exception e) {

            e.printStackTrace();
        }

        return result;

    }
}
